package br.ucb.cm.config.page.piece.ui;

import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.JToggleButton;
import java.awt.GridLayout;
import javax.swing.JLabel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.ImageIcon;

import br.ucb.cm.config.bean.Movimentation;

public class MovePanel extends JPanel {
	private static final long serialVersionUID = 1L;
	private String title;
	private JToggleButton bul;
	private JToggleButton bu;
	private JToggleButton bur;
	private JToggleButton br;
	private JToggleButton bl;
	private JToggleButton bdr;
	private JToggleButton bdl;
	private JToggleButton bd;
	private JToggleButton diagonalMax;
	private JToggleButton verticalMax;
	private JToggleButton horizontalMax;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JLabel label_4;
	
	public MovePanel(String title) {
		this.title = title;
		initComponents(title);
	}

	private void initComponents(String title) {
		setBorder(new TitledBorder(null, title, TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new GridLayout(0, 4, 0, 0));
		
		label = new JLabel("");
		add(label);
		
		verticalMax = new JToggleButton("1");
		verticalMax.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				refreshBtn(e);
			}
		});
		add(verticalMax);
		
		label_1 = new JLabel("");
		add(label_1);
		
		diagonalMax = new JToggleButton("1");
		diagonalMax.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				refreshBtn(e);
			}
		});

		add(diagonalMax);
		
		bul = new JToggleButton("");
		bul.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_ul.png")));
		add(bul);
		
		bu = new JToggleButton("");
		bu.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_up.png")));
		add(bu);
		
		bur = new JToggleButton("");
		bur.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_ur.png")));
		add(bur);
		
		label_2 = new JLabel("");
		add(label_2);
		
		bl = new JToggleButton("");
		bl.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_left.png")));
		add(bl);
		
		label_3 = new JLabel("");
		add(label_3);
		
		br = new JToggleButton("");
		br.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_right.png")));
		add(br);
		
		horizontalMax = new JToggleButton("1");
		horizontalMax.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				refreshBtn(e);
			}
		});
		add(horizontalMax);
		
		bdl = new JToggleButton("");
		bdl.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_dl.png")));
		add(bdl);
		
		bd = new JToggleButton("");
		bd.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_down.png")));
		add(bd);
		
		bdr = new JToggleButton("");
		bdr.setIcon(new ImageIcon(MovePanel.class.getResource("/br/ucb/cm/config/resources/control_dr.png")));
		add(bdr);
		
		label_4 = new JLabel("");
		add(label_4);
	}
	
	private void refreshBtn(ItemEvent event) {
		JToggleButton temp = ((JToggleButton)event.getSource());
		
		if(temp.getText().equals("1"))
			temp.setText("N");
		else
			temp.setText("1");
	}

	public void reset(){
		bu.setSelected(false);
		bd.setSelected(false);
		br.setSelected(false);
		bl.setSelected(false);
		bur.setSelected(false);
		bul.setSelected(false);
		bdr.setSelected(false);
		bdl.setSelected(false);

		verticalMax.setSelected(false);
		horizontalMax.setSelected(false);
		diagonalMax.setSelected(false);
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Movimentation getMovimentation(){
		return new Movimentation(
				bl.isSelected(),
				br.isSelected(), 
				bu.isSelected(), 
				bd.isSelected(), 
				bul.isSelected(), 
				bur.isSelected(), 
				bdl.isSelected(), 
				bdr.isSelected(), 
				verticalMax.isSelected(), 
				horizontalMax.isSelected(), 
				diagonalMax.isSelected()
		);
	}
}
